package com.ttg.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author huyangzou
 * @create 2021-03-18 15:13
 */
@Configuration
public class MybatisPlusConfig {

    @Value("${tlinx.database-type}")
    private String databaseType;

    /**
     * 数据库语言配置
     */
    @Bean
    public DatabaseIdProvider databaseIdProvider() {
        VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.put("Oracle","oracle");
        properties.put("MySQL","mysql");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        if ("oracle".equals(databaseType)) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
        } else {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        }

        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        Map<String, TableNameHandler> tableNameHandlerMap = new HashMap<>();
        tableNameHandlerMap.put("tbl_bank_private_primitive", new TblBankPrivatePrimitiveNameHandler());
        tableNameHandlerMap.put("tbl_bank_public_trade_primitive", new TblBankPublicTradePrimitiveNameHandler());
        tableNameHandlerMap.put("tbl_bank_public_primitive_aum", new TblBankPublicPrimitiveAumNameHandler());
        dynamicTableNameInnerInterceptor.setTableNameHandlerMap(tableNameHandlerMap);
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;

    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }

    class TblBankPrivatePrimitiveNameHandler implements TableNameHandler {

        @Override
        public String dynamicTableName(String sql, String tableName) {
            Map<String, String> tableSuffixMap = DynamicTableNameHelper.getTableSuffix();
            String tableSuffix = null;
            if (CollectionUtils.isNotEmpty(tableSuffixMap)) {
                tableSuffix = tableSuffixMap.get("tbl_bank_private_primitive");
                tableSuffixMap.remove("tbl_bank_private_primitive");
                DynamicTableNameHelper.setTableSuffix(tableSuffixMap);
            }

            if (StringUtils.isNotBlank(tableSuffix)) {
                return tableName + "_" + tableSuffix;
            }
            return tableName;
        }

    }

    class TblBankPublicTradePrimitiveNameHandler implements TableNameHandler {

        @Override
        public String dynamicTableName(String sql, String tableName) {
            Map<String, String> tableSuffixMap = DynamicTableNameHelper.getTableSuffix();
            String tableSuffix = null;
            if (CollectionUtils.isNotEmpty(tableSuffixMap)) {
                tableSuffix = tableSuffixMap.get("tbl_bank_public_trade_primitive");
                tableSuffixMap.remove("tbl_bank_public_trade_primitive");
                DynamicTableNameHelper.setTableSuffix(tableSuffixMap);
            }

            if (StringUtils.isNotBlank(tableSuffix)) {
                return tableName + "_" + tableSuffix;
            }
            return tableName;
        }

    }

    class TblBankPublicPrimitiveAumNameHandler implements TableNameHandler {

        @Override
        public String dynamicTableName(String sql, String tableName) {
            Map<String, String> tableSuffixMap = DynamicTableNameHelper.getTableSuffix();
            String tableSuffix = null;
            if (CollectionUtils.isNotEmpty(tableSuffixMap)) {
                tableSuffix = tableSuffixMap.get("tbl_bank_public_primitive_aum");
                tableSuffixMap.remove("tbl_bank_public_primitive_aum");
                DynamicTableNameHelper.setTableSuffix(tableSuffixMap);
            }

            if (StringUtils.isNotBlank(tableSuffix)) {
                return tableName + "_" + tableSuffix;
            }
            return tableName;
        }

    }

//    /**
//     * 序列生成器
//     */
//    @Bean
//    public OracleKeyGenerator oracleKeyGenerator(){
//        return new OracleKeyGenerator();
//    }
//
//    /**
//     * id生成配置
//     */
//    @Bean
//    public IdentifierGenerator idGenerator() {
//        return new CustomIdGenerator();
//    }
//
//    public class CustomIdGenerator implements IdentifierGenerator {
//        @Override
//        public Long nextId(Object entity) {
//            long nextId = Long.valueOf(RandomUtil.getSerialNumber(String.valueOf(System.currentTimeMillis()) + new Random().nextInt(100), 16));
//            return nextId;
//        }
//    }
}
